using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using NUnit.Framework;
using Wax;
using Wax.Provider;

namespace WaxLibTest.Provider
{
    [TestFixture]
    public class WaxExcelDataProviderTest
    {
        /// <summary>
        /// Tests the GetConnectionString method which should return a different connections string
        /// for *.xls files than for *.xlsx files.
        /// </summary>
        [Test]
        public void GetConnectionString()
        {
            FileInfo oldFileFormat = new FileInfo(Path.Combine(Environment.CurrentDirectory, @"Data\exampletest.xls"));
            FileInfo newFileFormat = new FileInfo(Path.Combine(Environment.CurrentDirectory, @"Data\exampletest.xlsx"));
            WaxExcelDataProvider excelDataProvider = (WaxExcelDataProvider) WaxDataManager.Providers["ExcelProvider"];
            string oldFileFormatConnectionString = excelDataProvider.GetConnectionString(oldFileFormat);
            string expectedOldConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;\";", oldFileFormat.FullName);
            string expectedNewConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;\";", newFileFormat.FullName);

            Assert.AreEqual(expectedOldConnectionString, oldFileFormatConnectionString);

            string newFileFormatConnectionString = excelDataProvider.GetConnectionString(newFileFormat);
            Assert.AreEqual(expectedNewConnectionString, newFileFormatConnectionString);
        }

        /// <summary>
        /// Test that the Excel data provider can parse a valid test script file.
        /// </summary>
        [Test]
        public void ParseValidFile()
        {
            FileInfo testFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, @"Data\SampleTest.xls"));
            WaxExcelDataProvider excelDataProvider = (WaxExcelDataProvider)WaxDataManager.Providers["ExcelProvider"];

            Collection<Test> tests = excelDataProvider.ParseFile(testFile.FullName);

            Assert.IsNotNull(tests, "WaxExcelDataProvider.ParseFile should not return null.");
            Assert.AreEqual(2, tests.Count, "Incorrect number of tests parsed.");

            // Assert the first test has been parsed correctly
            Test test1 = tests[0];
            Assert.AreEqual("Test1", test1.Id, "Test id not parsed correctly");
            Assert.AreEqual(4, test1.Commands.Count, string.Format("Incorrect number of tests in {0}.", test1.Id));

            // Assert the command's of the first test have been correctly parsed
            Command command = test1.Commands[0];
            Assert.AreEqual(CommandType.GoTo, command.CommandType, string.Format("Command type in command {0} in test {1} not correctly parsed.", command.Id, test1.Id));
            Assert.AreEqual(1, command.Arguments.Count, string.Format("Incorrrect number of command args in command {0} in test {1}.", command.Id, test1.Id));
            Assert.AreEqual("~testpage1.htm", command.Arguments[0], string.Format("Command args in command {0} in test {1} not correctly parsed.", command.Id, test1.Id));
            Assert.AreEqual(FindMethodTypes.None, command.FindMethod.MethodType, string.Format("Command find method type in command {0} in test {1} not correctly parsed.", command.Id, test1.Id));
        }

        /// <summary>
        /// Parses an invalid file to make sure the correct exception and message is thrown.
        /// </summary>
        [Test, ExpectedException(typeof(ParseException), ExpectedMessage = @"Invalid find method ""xxunknown"" found on row 4 column E.")]
        public void ParseInvalidFile()
        {
            FileInfo testFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, @"Data\InvalidSampleTest.xls"));
            WaxExcelDataProvider excelDataProvider = (WaxExcelDataProvider)WaxDataManager.Providers["ExcelProvider"];

            Collection<Test> tests = excelDataProvider.ParseFile(testFile.FullName);
        }
    }
}
